//
//  MoreTableViewController.m
//  JiaoWuGuanLi
//
//  Created by Zinzie on 14-4-27.
//  Copyright (c) 2014年 Zinzie. All rights reserved.
//

#import "MoreTableViewController.h"
#import "MainInterfaceAppDelegate.h"
#import "Student.h"
#import "Classes.h"
#import "Score.h"
#import "CoreData+MagicalRecord.h"


@interface MoreTableViewController ()<UIAlertViewDelegate>
    

@end

@implementation MoreTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}



-(void)setDengLuXueHao:(NSString *)dengLuXueHao{
    _dengLuXueHao = dengLuXueHao;
    if (_dengLuXueHao!=nil) {
        self.leftNoDisplay.title = _dengLuXueHao;
    }
    else{
        self.leftNoDisplay.title = @"未登录";
    }
    
}

-(UIBarButtonItem *)leftNoDisplay{
    if (!_leftNoDisplay) {
        _leftNoDisplay = [[UIBarButtonItem alloc]initWithTitle:@"未登录" style:UIBarButtonItemStyleBordered target:self action:@selector(noDisplay)];
        //        self.navigationItem.leftBarButtonItem = _leftNoDisplay;
        
    }
    return _leftNoDisplay;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = self.leftNoDisplay;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"退出" style:UIBarButtonItemStyleBordered target:self action:@selector(exitToLogin)];

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    MainInterfaceAppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    self.dengLuXueHao=myDelegate.userName;
    
    
}
-(void)noDisplay{
    
}
-(void)exitToLogin{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"" forKey:@"zhangHao"];
    [userDefaults setObject:@"" forKey:@"miMa"];
    [userDefaults setObject:@"" forKey:@"xingMing"];
    [userDefaults synchronize];
    
    NSUserDefaults *defaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.ZhangZhang.TodayExtensionSharingDefaults"];
    [defaults setObject:nil forKey:@"ClassesArray"];
    [defaults setBool:NO forKey:@"logged"];
    NSLog(@"log out %@",[userDefaults objectForKey:@"zhangHao"]);
    [self.navigationController.navigationController popToRootViewControllerAnimated:YES];
    AFHTTPRequestOperationManager  *manager=[AFHTTPRequestOperationManager manager];
    manager.cookieDictionary=nil;
  
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MainInterfaceAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    if (indexPath.section == 0 && indexPath.row == 1){
        
    }
    else if (indexPath.row==0&&indexPath.section==1) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString  stringWithFormat:@"\n是否确定删除当前账号(%@)的本地数据?\n删除后将无法离线查看此账号信息。",appDelegate.XueHao] message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        alert.tag = 1;
        [alert show];

    }else if(indexPath.row==1&&indexPath.section==1){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"\n是否确定删除所有本地数据?\n删除后将无法离线查看信息。" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        alert.tag = 2;
        [alert show];
        
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //NSLog(@"buttonIndex %d",buttonIndex);
    if ((alertView.tag == 1 || alertView.tag ==2) && buttonIndex == 1) {
    if (alertView.tag == 1) {
        MainInterfaceAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
        NSPredicate *everyclass= [NSPredicate predicateWithFormat:@"ofWho.xueHao == %@",appDelegate.XueHao];
        NSPredicate *predicateStudent = [NSPredicate predicateWithFormat:@"xueHao == %@",appDelegate.XueHao];
        for (Classes *classes in [Classes MR_findAllWithPredicate:everyclass]) {
            [classes MR_deleteEntity];
        };
        for (Score *score in [Score MR_findAllWithPredicate:everyclass]) {
            [score MR_deleteEntity];
        };
        for (Student *student in [Student MR_findAllWithPredicate:predicateStudent]) {
            [student MR_deleteEntity];
        };
        NSLog(@"删除当前数据 %d",[Classes MR_findAll].count);
         [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
        
    }else if(alertView.tag == 2){
        [Student MR_truncateAll];
        [Classes MR_truncateAll];
        [Score MR_truncateAll];
        NSLog(@"删除全部数据 %d",[Student MR_findAll].count);
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    }
       UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"删除成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        alert.tag = 99;
        [alert show];
    }
    else if(alertView.tag==99){
        
        [self exitToLogin];
    
    }
    
}


@end
